package com.tony.lock;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @author tonywang
 * @date 8/15/2018
 */
public class CyclicBarrierTest {
//    static CyclicBarrier cyclicBarrier = new CyclicBarrier(2);
    static CyclicBarrier cyclicBarrier = new CyclicBarrier(2, new Runnable() {
    @Override
    public void run() {
        System.out.println("4");
    }
});
    public static void main(String[] args) throws BrokenBarrierException, InterruptedException {
        new Thread(()-> {
            System.out.println("1");
            try {
                cyclicBarrier.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
            System.out.println("2");
        }).start();
        cyclicBarrier.await();
        System.out.println("3");
    }
}
